﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.Core.Library;

public partial class Admin_Hurman_Controls_Organize : System.Web.UI.UserControl
{
    public Guid Id { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
            loadCombo();

            CookieManager.deleteCookie(Page, "fbox");
        }
    }

    void loadCombo()
    {
        ddlBranch.DataTextField = "BranhName";
        ddlBranch.DataValueField = "Id";
        ddlBranch.DataSource = HrBranchManager.CreateInstant().GetAllBranch();
        ddlBranch.DataBind();

        ddlDepart.DataTextField = "DepartName";
        ddlDepart.DataValueField = "Id";
        ddlDepart.DataSource = MaDepartmentManager.CreateInstant().GetAllDepart();
        ddlDepart.DataBind();

        ddlJob.DataTextField = "JobName";
        ddlJob.DataValueField = "Id";
        ddlJob.DataSource = HrJobTitleManager.CreateInstant().GetAllJob();
        ddlJob.DataBind();
    }

    void loadData()
    {
        rep.DataSource = HrHumanOrganizeManager.CreateInstant().GetOrganize();
        rep.DataBind();
    }

    HrHumanOrganizeEntity getObj()
    {
        HrHumanOrganizeEntity obj = new HrHumanOrganizeEntity();
        obj.Id = Guid.NewGuid();
        obj.BranchId = new Guid(ddlBranch.SelectedValue);
        obj.DepartId = new Guid(ddlDepart.SelectedValue);
        obj.JobId = new Guid(ddlJob.SelectedValue);
        obj.UpdatedBy = Util.CurrentUserName;
        obj.UpdatedDate = DateTime.Now;
        return obj;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        CookieManager.setCookie(Page, "fbox", "add");
        if (Page.IsValid)
        {
            HrHumanOrganizeEntity obj = getObj();
            obj.CreatedBy = Util.CurrentUserName;
            obj.CreatedDate = DateTime.Now;
            HrHumanOrganizeManager.CreateInstant().Insert(obj);

            Response.Redirect(Request.RawUrl);
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        CookieManager.setCookie(Page, "fbox", "add");
        if (Page.IsValid)
        {
            HrHumanOrganizeEntity obj = getObj();
            HrHumanOrganizeEntity tmp = HrHumanOrganizeManager.CreateInstant().SelectOne(new Guid(this.ViewState["id"].ToString()));
            if (tmp != null)
            {
                obj.Id = tmp.Id;
                obj.CreatedBy = tmp.CreatedBy;
                obj.CreatedDate = tmp.CreatedDate;

                HrHumanOrganizeManager.CreateInstant().Update(obj);
                Response.Redirect(Request.RawUrl);
            }
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
    
    protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        HrHumanOrganizeEntity obj;
        switch (e.CommandName)
        {
            case "edit":
                CookieManager.setCookie(Page, "fbox", "add");
                this.ViewState["id"] = new Guid(e.CommandArgument.ToString());
                btnAdd.Visible = false;
                btnEdit.Visible = true;

                obj = HrHumanOrganizeManager.CreateInstant().SelectOne(new Guid(e.CommandArgument.ToString()));
                if (obj != null)
                {
                    ddlBranch.SelectedValue = obj.BranchId.ToString();
                    ddlDepart.SelectedValue = obj.DepartId.ToString();
                    ddlJob.SelectedValue = obj.JobId.ToString();
                }

                break;
            case "del":
                obj = HrHumanOrganizeManager.CreateInstant().SelectOne(new Guid(e.CommandArgument.ToString()));
                if (obj != null)
                {
                    obj.IsDeleted = true;
                    obj.UpdatedBy = Util.CurrentUserName;
                    obj.UpdatedDate = DateTime.Now;

                    HrHumanOrganizeManager.CreateInstant().Update(obj);

                    Response.Redirect(Request.RawUrl);
                }
                break;
        }
    }

    protected void valid_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
        if (this.ViewState["id"] != null) // edit
        {
            if (HrHumanOrganizeManager.CreateInstant().CheckExists(new Guid(this.ViewState["id"].ToString()), new Guid(ddlBranch.SelectedValue), new Guid(ddlDepart.SelectedValue), new Guid(ddlJob.SelectedValue)))
            {
                valid.ErrorMessage = "Trùng thông tin";
                args.IsValid = false;
            }
        }
        else
        {
            if (HrHumanOrganizeManager.CreateInstant().CheckExists(Guid.Empty, new Guid(ddlBranch.SelectedValue), new Guid(ddlDepart.SelectedValue), new Guid(ddlJob.SelectedValue)))
            {
                valid.ErrorMessage = "Trùng thông tin";
                args.IsValid = false;
            }
        }
    }
}
